package com.shanghaionstar.user;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.onstar.cn.auth.userProfile.FeatureEnum;
import com.onstar.cn.common.bean.EnvMode;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.smart.entity.ServiceItem;
import com.onstar.cn.weight.ToastView;
import com.onstar.cn.weight.more.CustomLineLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.life.AlarmManagerUtil;
import com.shanghaionstar.notifysetting.SetNotifyActivity;
import com.shanghaionstar.report.bean.ReportReqParam;
import com.shanghaionstar.user.iview.IOptViewImpl;
import com.shanghaionstar.user.presenter.OptPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.AuthorUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.DevelopServerEnvironmentUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.JoinMeConstants;
import com.shanghaionstar.weight.AgreementDialog;
import com.shanghaionstar.weight.CommonProgressButton;

import java.io.File;
import java.util.List;

/**
 * Created by 7828wc on 16/6/30.
 * 设置界面
 */
public class SettingFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {
    @FindViewById
    private ToggleButton toggle_voice, toggle_vibrate, toggle_ovd_refresh, toggle_mro_setting, toggle_behavior, toggle_carAssess, toggle_ubi;
    @FindViewById
    private CustomLineLayout preferred_setting_layout, cll_setNotify;
    @FindViewById
    private RelativeLayout mro_service_layout, driver_behavior_layout, driver_car_assess_layout, vehicle_ubi_layout;
    @FindViewById
    private ImageView red_point;
    @FindViewById
    private CommonProgressButton btn_change, btn_exit, btn_clear_cache;
    @FindViewById
    private TextView tv_carAssess_status;

    /**
     * 评价服务presenter
     */
    private OptPresenter optPresenter;

    private int retryTimes = 0;

    public OptPresenter getOptPresenter() {
        if (optPresenter == null) {
            optPresenter = new OptPresenter(baseActivity.getScheduler(), baseActivity);
            optPresenter.setOptView(new IOptViewImpl() {
//                @Override
//                public void onTurnOnSuccess(BaseResponse response) {
//                    BusinessUtil.sendReportByRequestParams(getContext(), FunctionNameFindId.SmartDriver_SettingAgree.getStringValue(), ReportReqParam.getSmartDriveSettingReqParam(FunctionNameFindId.SmartDriver_SettingAgree, true));
//                    PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, true);
//                    toggle_behavior.setChecked(true);
//                    dismissDialog(progressDialog);
//                }
//
//                @Override
//                public void onTurnOffSuccess(BaseResponse response) {
//                    BusinessUtil.sendReportByRequestParams(getContext(), FunctionNameFindId.SmartDriver_SettingReject.getStringValue(), ReportReqParam.getSmartDriveSettingReqParam(FunctionNameFindId.SmartDriver_SettingReject, true));
//                    PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, false);
//                    toggle_behavior.setChecked(false);
//                    dismissDialog(progressDialog);
//                }
//
//                @Override
//                public void onFailed(Throwable throwable) {
//                    toggle_behavior.toggle();
//                    //根据之前的toggle状态决定操作方式
//                    FunctionNameFindId functionNameFindId = toggle_behavior.isChecked() ? FunctionNameFindId.SmartDriver_SettingReject : FunctionNameFindId.SmartDriver_SettingAgree;
//                    BusinessUtil.sendReportByRequestParams(getContext(), functionNameFindId.getStringValue(), ReportReqParam.getSmartDriveSettingReqParam(functionNameFindId, false, throwable == null ? "" : throwable.getMessage()));
//
//                    dismissDialog(progressDialog);
//                    if (throwable != null && !TextUtils.isEmpty(throwable.getMessage())) {
//                        new ToastView(baseActivity).showToast(throwable.getMessage());
//                    }
//                }
//
//                @Override
//                public void onTurnOnCarAssessSuccess(BaseResponse response) {
//                    toggle_carAssess.setChecked(true);
//                    dismissDialog(progressDialog);
//                }
//
//                @Override
//                public void onTurnOffCarAssessSuccess(BaseResponse response) {
//                    toggle_carAssess.setChecked(false);
//                    dismissDialog(progressDialog);
//                }
//
//                @Override
//                public void onTurnCarAssessFailed(Throwable throwable) {
//                    toggle_carAssess.toggle();
//                    dismissDialog(progressDialog);
//                    if (throwable != null && !TextUtils.isEmpty(throwable.getMessage())) {
//                        new ToastView(baseActivity).showToast(throwable.getMessage());
//                    }
//                }
//
//                @Override
//                public void showCarAssessAgreement(Boolean show) {
//                    driver_behavior_layout.setVisibility(show ? View.VISIBLE : View.GONE);
//                    ServiceItem item = OnstarApplication.getApplication().getCarAssessStatusItem();
//                    if (item == null) {
//                        if (retryTimes < 3) {
//                            retryTimes = retryTimes + 1;
//                            getOptPresenter().getCarAssessStatus(getContext());
//                        }
//                    } else {
//                        toggle_carAssess.setChecked(ServiceItem.isCarAssessOpen(item));
//                        if (ServiceItem.isCarAssessInDeveloping(item)) {
//                            tv_carAssess_status.setText(R.string.in_developing);
//                            toggle_carAssess.setEnabled(false);
//                        }
//                    }
//                }

                @Override
                public void onGetServiceItem(List<ServiceItem> items) {
                    super.onGetServiceItem(items);
                    if (items == null) {
                        if (retryTimes < 3) {
                            retryTimes = retryTimes + 1;
                            getOptPresenter().getServiceStatus(getContext());
                        }
                    } else {
                        handleService();
                    }
                }

                @Override
                public void onSwitchService(String name, boolean in, boolean isSuc, Throwable throwable) {
                    super.onSwitchService(name, in, isSuc, throwable);
                    dismissDialog(progressDialog);
                    ServiceItem item = getOptPresenter().getServiceItemByName(name);
                    if (isSuc)
                        item.setOptStatus(in ? "1" : "0");
                    if (name.equalsIgnoreCase(OptPresenter.CAR_ASSESSMENT)) {
                        if (isSuc) {
                            toggle_carAssess.setChecked(in);

                        } else {
                            toggle_carAssess.toggle();
                            if (throwable != null && !TextUtils.isEmpty(throwable.getMessage())) {
                                new ToastView(baseActivity).showToast(throwable.getMessage());
                            }
                        }
                    } else if (name.equalsIgnoreCase(OptPresenter.SMART_DRIVE)) {
                        if (isSuc) {
                            toggle_behavior.setChecked(in);
                            PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, in);
                            BusinessUtil.sendReportByRequestParams(getContext(), in ?
                                            FunctionNameFindId.SmartDriver_SettingAgree.getStringValue() : FunctionNameFindId.SmartDriver_SettingReject.getStringValue(),
                                    ReportReqParam.getSmartDriveSettingReqParam(in ? FunctionNameFindId.SmartDriver_SettingAgree : FunctionNameFindId.SmartDriver_SettingReject, true));
                        } else {
                            toggle_behavior.toggle();
                            FunctionNameFindId functionNameFindId = toggle_behavior.isChecked() ? FunctionNameFindId.SmartDriver_SettingReject : FunctionNameFindId.SmartDriver_SettingAgree;
                            BusinessUtil.sendReportByRequestParams(getContext(), functionNameFindId.getStringValue(), ReportReqParam.getSmartDriveSettingReqParam(functionNameFindId, false, throwable == null ? "" : throwable.getMessage()));
                            if (throwable != null && !TextUtils.isEmpty(throwable.getMessage())) {
                                new ToastView(baseActivity).showToast(throwable.getMessage());
                            }
                        }
                    } else if (name.equalsIgnoreCase(OptPresenter.FUEL_ECONOMY)) {
                        if (isSuc) {
                            //统计油耗排名开关
                            BusinessUtil.sendReportByClickType(baseActivity, in ? FunctionNameFindId.Setting_FuelEconomyOn.getStringValue() : FunctionNameFindId.Setting_FuelEconomyOff.getStringValue());
                            toggle_ubi.setChecked(in);
                        } else {
                            toggle_ubi.toggle();
                            if (throwable != null && !TextUtils.isEmpty(throwable.getMessage())) {
                                new ToastView(baseActivity).showToast(throwable.getMessage());
                            }
                        }
                    }

                }
            });


        }
        return optPresenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.more_setting;
    }

    @Override
    public void onResume() {
        super.onResume();
        preferred_setting_layout.setRightText(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.TBT_CHOOSE_SAVE, true) ? getString(R.string.pilot_audio) : getString(R.string.navigator_car));

    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (isFirstLoad()) {
            preferred_setting_layout.setVisibility(ApplicationUtil.getInstance().isLogin() && BusinessUtil.isServiceFeature(FeatureEnum.SENDNAVDESTINATION.value(), getActivity()) ? View.VISIBLE : View.GONE);
            //            red_point.setVisibility(BusinessUtil.isMroShow(getActivity()) ? View.VISIBLE : View.GONE);
            mro_service_layout.setVisibility(BusinessUtil.isMroShow(getActivity()) ? View.VISIBLE : View.GONE);
            //     driver_behavior_layout.setVisibility(ApplicationUtil.getInstance().isLogin()&&ApplicationUtil.getInstance().isGen10()&&!ApplicationUtil.getInstance().getLoginRole().equalsIgnoreCase(JoinMeConstants.VISITOR)?View.VISIBLE: View.GONE);
//            ServiceItem carAssessItem = OnstarApplication.getApplication().getCarAssessStatusItem();

            List<ServiceItem> serviceItems = ApplicationUtil.getInstance().getServiceItems();
            if (ApplicationUtil.getInstance().isLogin() && serviceItems == null) {
                getOptPresenter().getServiceStatus(getActivity());
            }
//            else{
//                Log.e("BaseRequest", GsonHelper.toJson(serviceItems));
//            }

            handleService();

            btn_change.setVisibility(DevelopServerEnvironmentUtil.getInstance().isDevelopMode(getActivity()) ? View.VISIBLE : View.GONE);
            int environment = DevelopServerEnvironmentUtil.getInstance().getCurrentEnvironmentMode(getActivity());
            btn_change.setText(CommonUtil.isNull(environment) ? "Default" : "Current:" + EnvMode.modes.valueAt(environment));


            toggle_voice.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.IS_PALY_SOUND, true));
            toggle_behavior.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, true));
            toggle_vibrate.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.IS_VIBARTOR, true));
            toggle_ovd_refresh.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.OVD_AUTO_REFRESH, false));
            toggle_mro_setting.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.IS_USE_MRO(getActivity()), true));


            preferred_setting_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((MoreActivity) getActivity()).changeFragment(ChooseTBTODDFragment.class.getName());
                }
            });

            btn_change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setItems(EnvMode.modesLst, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            DevelopServerEnvironmentUtil.getInstance().setEnvironmentMode(i, getActivity());
                            btn_change.setText("Current:" + EnvMode.modes.valueAt(i));
                        }
                    });
                    builder.create().show();
                }
            });

            btn_exit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning, R.string.exit_string, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_Logoff.getStringValue());//设置_点击退出
                            AuthorUtil.getInstance().loginOut(getActivity());
                            AlarmManagerUtil.cancelAllAlram(getActivity());
                            back();
//                            System.exit(0);
//                            MainTabActivity.startActivity(getActivity());
                        }
                    }, true);

                }
            });

            btn_clear_cache.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning, R.string.more_setting_clear_cache_confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_ClearCache.getStringValue());//设置_点击清除缓存数据
                            ImageLoader.getInstance().clearDiscCache();
                            ImageLoader.getInstance().clearMemoryCache();
                            CommonUtil.removeMapCache(new File("sdcard/AMap"));
                            new ToastView(getActivity()).showToast(getString(R.string.more_setting_clear_cache_success));
                        }
                    }, true);
                }
            });


            btn_exit.setVisibility(ApplicationUtil.getInstance().

                    isLogin()

                    ? View.VISIBLE : View.GONE);

//        toggle_behavior.setOnCheckedChangeListener(this);
            toggle_vibrate.setOnCheckedChangeListener(this);
            toggle_ovd_refresh.setOnCheckedChangeListener(this);
            toggle_mro_setting.setOnCheckedChangeListener(this);
            toggle_voice.setOnCheckedChangeListener(this);
            toggle_carAssess.setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View v) {
                                                        if (toggle_carAssess.isChecked()) {
                                                            showLoading();
                                                            getOptPresenter().optIn(OptPresenter.CAR_ASSESSMENT);
                                                            BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_CarAssmtSeviceProtocolOn.getStringValue());//设置_爱车评估服务协议开启
                                                        } else {
                                                            showLoading();
                                                            getOptPresenter().optOut(OptPresenter.CAR_ASSESSMENT);
                                                            BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_CarAssmtSeviceProtocolOff.getStringValue());//设置_爱车评估服务协议关闭
                                                        }
                                                    }
                                                }

            );
            toggle_behavior.setOnClickListener(new View.OnClickListener()

                                               {
                                                   @Override
                                                   public void onClick(View v) {
                                                       if (toggle_behavior.isChecked())
                                                           showAgreeDialog();
                                                       else {
                                                           AlertDialogUtil.showSmartSettingAlertDialog(
                                                                   getActivity(),
                                                                   getActivity().getString(R.string.dialog_warning),
                                                                   getActivity().getString(R.string.setting_smart_msg),
                                                                   getActivity().getString(R.string.setting_smart_okbtn),
                                                                   getActivity().getString(R.string.setting_smart_cancelbtn),
                                                                   new DialogInterface.OnClickListener() {
                                                                       @Override
                                                                       public void onClick(DialogInterface dialog, int which) {
                                                                           showLoading();
                                                                           BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_SmartDriverOff.getStringValue());//设置_驾驶行为评价服务关闭
                                                                           BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_SmartDriver_ppwdwclickOK.getStringValue());//设置_驾驶行为评价关闭弹框点击忍痛关闭
                                                                           getOptPresenter().optOut(OptPresenter.SMART_DRIVE);
                                                                           dialog.dismiss();
                                                                       }
                                                                   },
                                                                   new DialogInterface.OnClickListener() {
                                                                       @Override
                                                                       public void onClick(DialogInterface dialog, int which) {
                                                                           BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_SmartDriver_ppwdwclickCancel.getStringValue());//设置_驾驶行为评价关闭弹框点击算了不关了
                                                                           toggle_behavior.setChecked(!toggle_behavior.isChecked());
                                                                           AlertDialogUtil.showAlertDialog(getActivity(),
                                                                                   getActivity().getString(R.string.dialog_warning),
                                                                                   getActivity().getString(R.string.setting_smart_cancel_msg));
                                                                           dialog.dismiss();
                                                                       }
                                                                   }
                                                           );
                                                       }
                                                   }
                                               }

            );
            toggle_ubi.setOnClickListener(new View.OnClickListener()

                                          {
                                              @Override
                                              public void onClick(View v) {
                                                  if (toggle_ubi.isChecked()) {
                                                      AlertDialogUtil.showAlertDialog(getActivity(),
                                                              getString(R.string.dialog_warning),
                                                              getString(R.string.setting_ubi_on_msg),
                                                              true,
                                                              new DialogInterface.OnClickListener() {
                                                                  @Override
                                                                  public void onClick(DialogInterface dialog, int which) {
                                                                      showLoading();
                                                                      getOptPresenter().optIn(OptPresenter.FUEL_ECONOMY);
                                                                      dialog.dismiss();
                                                                  }
                                                              });
                                                  } else {
                                                      AlertDialogUtil.showSmartSettingAlertDialog(
                                                              getActivity(),
                                                              getString(R.string.dialog_warning),
                                                              getString(R.string.setting_ubi_off_msg),
                                                              getString(R.string.setting_smart_okbtn),
                                                              getString(R.string.setting_smart_cancelbtn),
                                                              new DialogInterface.OnClickListener() {
                                                                  @Override
                                                                  public void onClick(DialogInterface dialog, int which) {
                                                                      BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.Setting_FuelEconomyOff_ppwOK.getStringValue());
                                                                      showLoading();
                                                                      getOptPresenter().optOut(OptPresenter.FUEL_ECONOMY);
                                                                      dialog.dismiss();
                                                                  }
                                                              },
                                                              new DialogInterface.OnClickListener() {
                                                                  @Override
                                                                  public void onClick(DialogInterface dialog, int which) {
                                                                      BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.Setting_FuelEconomyOff_ppwOff.getStringValue());
                                                                      toggle_ubi.setChecked(!toggle_ubi.isChecked());
//                                                                      AlertDialogUtil.showAlertDialog(getActivity(),
//                                                                              getActivity().getString(R.string.dialog_warning),
//                                                                              getActivity().getString(R.string.setting_smart_cancel_msg));
//                                                                      dialog.dismiss();
                                                                      new AlertDialog.Builder(baseActivity)
                                                                              .setMessage(getActivity().getString(R.string.setting_smart_cancel_msg))
                                                                              .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                                                  @Override
                                                                                  public void onClick(DialogInterface dialog, int which) {
                                                                                      BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.Setting_FuelEconomyOn_ppwIknow.getStringValue());
                                                                                  }
                                                                              }).show();
                                                                  }
                                                              }
                                                      );
                                                  }
                                              }
                                          }

            );
            cll_setNotify.setVisibility(ApplicationUtil.getInstance().

                    isLogin()

                    && ApplicationUtil.getInstance().getLoginRole()
                    .equalsIgnoreCase(Constants.OWNER)

                    ? View.VISIBLE : View.GONE);
            cll_setNotify.setOnClickListener(new View.OnClickListener()

                                             {
                                                 @Override
                                                 public void onClick(View v) {
                                                     BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_NotificationSettings.getStringValue());//设置_点击退出
                                                     startActivity(new Intent(baseActivity, SetNotifyActivity.class));
                                                 }
                                             }

            );
        }
    }

//    && ServiceItem.isCarAssessAvailable(carAssessItem)

    private void handleService() {
        ServiceItem carAssessItem = getOptPresenter().getServiceItemByName(OptPresenter.CAR_ASSESSMENT);

        driver_car_assess_layout.setVisibility(
                ApplicationUtil.getInstance().isLogin()
                        && ApplicationUtil.getInstance().getLoginRole().equalsIgnoreCase(JoinMeConstants.OWNER)
                        ? View.VISIBLE : View.GONE);

        if (carAssessItem != null)
            toggle_carAssess.setChecked(ServiceItem.isCarAssessOpen(carAssessItem));
        else
            toggle_carAssess.setChecked(false);

        if (ServiceItem.isCarAssessInDeveloping(carAssessItem)) {
            tv_carAssess_status.setText(R.string.in_developing);
            toggle_carAssess.setEnabled(false);
        }


        ServiceItem item = getOptPresenter().getServiceItemByName(OptPresenter.SMART_DRIVE);
        driver_behavior_layout.setVisibility(
                ApplicationUtil.getInstance().isLogin()
                        && !ApplicationUtil.getInstance().isPhev()
                        && ApplicationUtil.getInstance().getLoginRole().equalsIgnoreCase(JoinMeConstants.OWNER)
                        && (item != null && item.availability != null && item.availability.equalsIgnoreCase("1")) ? View.VISIBLE : View.GONE);


        ServiceItem fuelEconomyItem = getOptPresenter().getServiceItemByName(OptPresenter.FUEL_ECONOMY);
        boolean isCanShowFuel = fuelEconomyItem != null && fuelEconomyItem.availability != null && fuelEconomyItem.availability.equalsIgnoreCase("1");

        vehicle_ubi_layout.setVisibility(ApplicationUtil.getInstance().isLogin() && isCanShowFuel ? View.VISIBLE : View.GONE);

        if (fuelEconomyItem != null) {
            toggle_ubi.setChecked(ServiceItem.isOpen(fuelEconomyItem));
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.toggle_voice:
                BusinessUtil.sendReportByClickType(getActivity(), isChecked ? FunctionNameFindId.Setting_VoiceOn.getStringValue() : FunctionNameFindId.Setting_VoiceOff.getStringValue());
                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.IS_PALY_SOUND, isChecked);
                break;
            case R.id.toggle_vibrate:
                BusinessUtil.sendReportByClickType(getActivity(), isChecked ? FunctionNameFindId.Setting_VibrateOn.getStringValue() : FunctionNameFindId.Setting_VibrateOff.getStringValue());
                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.IS_VIBARTOR, isChecked);
                break;
            case R.id.toggle_ovd_refresh:
                BusinessUtil.sendReportByClickType(getActivity(), isChecked ? FunctionNameFindId.Setting_LoginRefreshOn.getStringValue() : FunctionNameFindId.Setting_LoginRefreshOff.getStringValue());
                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.OVD_AUTO_REFRESH, isChecked);
                break;
            case R.id.toggle_mro_setting:
                BusinessUtil.sendReportByClickType(getActivity(), isChecked ? FunctionNameFindId.Setting_OAssistantOn.getStringValue() : FunctionNameFindId.Setting_OAssistantOn.getStringValue());
                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.IS_USE_MRO(getActivity()), isChecked);
                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.IS_SETTING_MRO_CLICK, true);
                red_point.setVisibility(View.GONE);
                break;
            case R.id.toggle_behavior:
//                if (isChecked)
//                    showAgreeDialog();
//                else {
//                    showLoading();
//                    getOptPresenter().turnOff();
//                }
//                    PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, isChecked);
//                if (isChecked) {
//                    AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning, R.string.driver_behavior, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                        }
//                    });
//                }
//                PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, isChecked);
                break;
            default:
                break;
        }
    }

    ProgressDialog progressDialog;

    private void showLoading() {
        progressDialog = showLoadProgress();
        progressDialog.show();
    }

    /**
     * 驾驶评分协议，车况检测报告分析协议对话框
     */
    AgreementDialog agreementDialog, carAssessDialog;


    private void showAgreeDialog() {
        if (agreementDialog == null) {
            agreementDialog = new AgreementDialog(baseActivity);
            agreementDialog.setCancelListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PreferencesUtls.getInstance(getActivity()).putBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, false);
                    toggle_behavior.setChecked(false);
                    agreementDialog.dismiss();
                }
            });
            agreementDialog.setConfirmListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    agreementDialog.dismiss();
                    showLoading();
                    BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_SmartDriverOn.getStringValue());//设置_驾驶行为评价服务开启
                    getOptPresenter().optIn(OptPresenter.SMART_DRIVE);
                }
            });
            agreementDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    toggle_behavior.setChecked(PreferencesUtls.getInstance(getActivity()).getBoolean(FinalConstants.SharedPreferencesConfig.AGREED_BEHAVIOR, true));
                }
            });
        }
        if (baseActivity != null && !baseActivity.isFinishing())
            agreementDialog.show();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.Setting_back.getStringValue());//设置_驾驶行为评价服务开启
    }
}
